/**
 * 路由配置
 */
import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/views/Login.vue'),
      meta: { title: '登录', requiresAuth: false }
    },
    {
      path: '/',
      component: () => import('@/layouts/MainLayout.vue'),
      redirect: '/dashboard',
      meta: { requiresAuth: true },
      children: [
        {
          path: 'dashboard',
          name: 'Dashboard',
          component: () => import('@/views/Dashboard.vue'),
          meta: { title: '数据面板', icon: 'DataBoard' }
        },
        // 轮播图管理
        {
          path: 'banner',
          name: 'Banner',
          component: () => import('@/views/banner/BannerList.vue'),
          meta: { title: '轮播图管理', icon: 'Picture' }
        },
        // 活动管理
        {
          path: 'activity',
          name: 'Activity',
          redirect: '/activity/list',
          meta: { title: '活动管理', icon: 'Calendar' },
          children: [
            {
              path: 'list',
              name: 'ActivityList',
              component: () => import('@/views/activity/ActivityList.vue'),
              meta: { title: '活动列表' }
            },
            {
              path: 'create',
              name: 'ActivityCreate',
              component: () => import('@/views/activity/ActivityForm.vue'),
              meta: { title: '创建活动' }
            },
            {
              path: 'edit/:id',
              name: 'ActivityEdit',
              component: () => import('@/views/activity/ActivityForm.vue'),
              meta: { title: '编辑活动' }
            },
            {
              path: 'registrations/:id',
              name: 'ActivityRegistrations',
              component: () => import('@/views/activity/ActivityRegistrations.vue'),
              meta: { title: '报名管理' }
            }
          ]
        },
        // 红娘管理
        {
          path: 'matchmaker',
          name: 'Matchmaker',
          redirect: '/matchmaker/list',
          meta: { title: '红娘管理', icon: 'User' },
          children: [
            {
              path: 'list',
              name: 'MatchmakerList',
              component: () => import('@/views/matchmaker/MatchmakerList.vue'),
              meta: { title: '红娘列表' }
            },
            {
              path: 'create',
              name: 'MatchmakerCreate',
              component: () => import('@/views/matchmaker/MatchmakerForm.vue'),
              meta: { title: '添加红娘' }
            },
            {
              path: 'edit/:id',
              name: 'MatchmakerEdit',
              component: () => import('@/views/matchmaker/MatchmakerForm.vue'),
              meta: { title: '编辑红娘' }
            }
          ]
        },
        // 课程管理
        {
          path: 'course',
          name: 'Course',
          redirect: '/course/list',
          meta: { title: '课程管理', icon: 'Reading' },
          children: [
            {
              path: 'list',
              name: 'CourseList',
              component: () => import('@/views/course/CourseList.vue'),
              meta: { title: '课程列表' }
            },
            {
              path: 'create',
              name: 'CourseCreate',
              component: () => import('@/views/course/CourseForm.vue'),
              meta: { title: '创建课程' }
            },
            {
              path: 'edit/:id',
              name: 'CourseEdit',
              component: () => import('@/views/course/CourseForm.vue'),
              meta: { title: '编辑课程' }
            }
          ]
        },
        // 成功案例管理
        {
          path: 'success-case',
          name: 'SuccessCase',
          redirect: '/success-case/list',
          meta: { title: '成功案例', icon: 'TrophyBase' },
          children: [
            {
              path: 'list',
              name: 'SuccessCaseList',
              component: () => import('@/views/success-case/SuccessCaseList.vue'),
              meta: { title: '案例列表' }
            },
            {
              path: 'create',
              name: 'SuccessCaseCreate',
              component: () => import('@/views/success-case/SuccessCaseForm.vue'),
              meta: { title: '创建案例' }
            },
            {
              path: 'edit/:id',
              name: 'SuccessCaseEdit',
              component: () => import('@/views/success-case/SuccessCaseForm.vue'),
              meta: { title: '编辑案例' }
            }
          ]
        },
        // 用户管理
        {
          path: 'user',
          name: 'User',
          redirect: '/user/list',
          meta: { title: '用户管理', icon: 'UserFilled' },
          children: [
            {
              path: 'list',
              name: 'UserList',
              component: () => import('@/views/user/UserList.vue'),
              meta: { title: '用户列表' }
            },
            {
              path: 'vip',
              name: 'UserVip',
              component: () => import('@/views/user/UserVipList.vue'),
              meta: { title: 'VIP用户' }
            }
          ]
        },
        // 动态管理
        {
          path: 'dynamic',
          name: 'Dynamic',
          component: () => import('@/views/dynamic/DynamicList.vue'),
          meta: { title: '动态管理', icon: 'ChatDotSquare' }
        }
      ]
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'NotFound',
      component: () => import('@/views/404.vue'),
      meta: { title: '404' }
    }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  
  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - 婚恋管理系统` : '婚恋管理系统'
  
  // 检查是否需要认证
  if (to.meta.requiresAuth !== false) {
    if (!userStore.token) {
      next('/login')
    } else {
      next()
    }
  } else {
    // 如果已登录，访问登录页则跳转到首页
    if (to.path === '/login' && userStore.token) {
      next('/')
    } else {
      next()
    }
  }
})

export default router
